package mysql

import (
	"furni-manage/goods/goods"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(50);not null;"`
	Price   float64 `gorm:"type:decimal(10,2)"`
	Num     int64   `gorm:"type:int(11)"`
	Image   string  `gorm:"type:varchar(30)"`
	ClassId int64   `gorm:"type:int(11)"`
	BrandId int64   `gorm:"type:int(11)"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoodsByname(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(g).Error
}

func (g *Goods) FindGoodsById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id", id).First(g).Error
}
func GoodsList(db *gorm.DB, data []*goods.GoodList) []*goods.GoodList {
	db.Model(Goods{}).
		Select(
			"goods.`name` AS goods_name",
			"goods.id AS goods_id",
			"goods.price",
			"goods.num",
			"goods.image",
			"classes.`name` AS classes_name",
			"goods.created_at").
		Joins("JOIN classes ON classes.id = goods.class_id").
		Find(&data)
	return data
}
